package com.easyUploader.entity.dao;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

import java.io.File;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * <p>
 *
 * </p>
 *
 * @author 大忽悠
 * @since 2022年11月01日
 */
import com.common.util.StringUtil;
import com.easyUploader.property.LocalFileProperty;
import com.easyUploader.util.FileUtil;
import com.easyUploader.util.ZipUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 大忽悠
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Slf4j
public class Bucket implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 存储桶名称
     */
    private String name;

    /**
     * 存储桶类型: 0-只读,1-读写
     */
    private Integer bucketType;

    /**
     * 访问类型: 0-私有,1-公开
     */
    private Integer accessType;

    /**
     * 创建者
     */
    private String createBy;

    /**
     * 更新者
     */
    private String updateBy;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createtime;


    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updatetime;

    public static File zipDirIfNeeded(String bucket, String name, LocalFileProperty localFileProperty) {
        //判断目标文件是否存在,如果存在那么是否为目录
        String targetFilePath = StringUtil.pathMerge(true,localFileProperty.getBaseDir(), bucket, name);
        return ZipUtil.zipIfNeeded(targetFilePath, localFileProperty.getZipTempDir());
    }

    public void doDeleteFiles(String baseDir) {
        FileUtil.deleteFile(StringUtil.pathMerge(true,baseDir,name));
    }

    /**
     * @param bucketType 大于0都默认为读写
     */
    public void setBucketType(Integer bucketType) {
        this.bucketType = bucketType>0?1:0;
    }
}
